/**
 * \file assembler.c
 * \brief Implementation of the RipeScript assembler.
 **/

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include "misc.h"
#include "standard.h"
#include "error.h"

int main(int argc, char** argv)
{
  /* Initialize the error-reporting system. */
  r_err_init(argv[0]);

  struct option long_options[] =
    {
      {"output", required_argument, 0, 'o'},
      {0, 0, 0, 0}
    };
  /* getopt_long stores the option index here. */
  int option_index = 0;

  while(true){
    int c = getopt_long(argc, argv, "o:", long_options, &option_index);

    /* End of long/short options, remainder of the arguments should be
     * interpreted like input files. */
    if (c == EOF){
      break;
    }

    switch(c){
      case 'o':
        if (optarg == NULL){
          r_error("Option '-o' requires an argument.");
          return 1;
        }
        break;
      default:
        r_error("Parsing command-line arguments failed.");
        return 1;
    }
  }

  if (optind >= argc){
    r_error("No input files specified.");
    return 1;
  }

  int arg = optind;
  while (arg < argc){
    char* file_buf;
    uint32_t file_size;
    int rv;
    rv = r_misc_loadfile(argv[arg], &file_buf, &file_size);
    if (rv != R_SUCCESS)
    {
      r_error("Failed to read '%s'.", argv[arg]);
      return 1;
    }
    arg++;
  }
  return 0;
}
